using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapManage : MonoBehaviour
{
    //定义外墙、地板
    public GameObject[] OutWallArray;
    public GameObject[] FloorArray;
    public GameObject[] Food;
    public GameObject[] Enemy;
    public GameObject[] EnemyHome;
    public GameObject[] Ston;
    public GameObject[] Water;
    public GameObject[] Tree;
    public GameObject[] House;
    public GameObject[] Soil;
    public GameObject[] Seed;

    //定义地图大小
    public int Rows = 10;//行
    public int Cols = 10;//列
    public int Num = 5;
    public int Count = 0;
    private Transform MapHolder;
    
    public int SoilType = 0;
    public Vector2 postion1;
    public Vector2 postion2;
    public Vector2 postion3;

    public float restTime = 4;
    public float restTime1 = 10;
    public float restTime2 = 15;
    public int rest = 0;
    private float restTimer = 0;
    private float restTimer1 = 0;
    private float restTimer2 = 0;

    // Start is called before the first frame update
    void Start()
    {
        InitMap();

    }

    // Update is called once per frame
    void Update()
    {
        if (rest>=1) { restTimer += Time.deltaTime; }
        
        restTimer1 += Time.deltaTime;
        restTimer2 += Time.deltaTime;
        if (restTimer > restTime)
        {

            restTimer = 0;

            ChangeFood();


        }
        if (restTimer1 > restTime1)
        {

            restTimer1 = 0;

            ChangeFood1();


        }
        if (restTimer2 > restTime2)
        {

            restTimer2 = 0;

            ChangeFood2();


        }
    }

    private void InitMap()
    {
        MapHolder = new GameObject("Map").transform;
        



        //创建外墙和地板
        for (int x = 0; x < Cols; x++)
        {
            for (int y = 0; y < Rows; y++)
            {
                if (x == 0 || y == 0 || x == Cols - 1 || y == Rows - 1)
                {

                    GameObject go = GameObject.Instantiate(OutWallArray[0], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    go.transform.SetParent(MapHolder);
                }
                else
                {
                    int ReFloor = Random.Range(0, 4);
                    GameObject go = GameObject.Instantiate(FloorArray[ReFloor], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    go.transform.SetParent(MapHolder);

                }

                Count++;
                if (Count <= Num)
                {
                    //创建敌人 食物
                    //先随机产生其位置


                    int p1x = Random.Range(1, Rows - 1);
                    int p1y = Random.Range(1, Cols - 1);
                    int p2x = Random.Range(1, Rows - 1);
                    int p2y = Random.Range(1, Cols - 1);
                    int p3x = Random.Range(1, Rows - 1);
                    int p3y = Random.Range(1, Cols - 1);
                    int p4x = Random.Range(1, Rows - 1);
                    int p4y = Random.Range(1, Cols - 1);
                    if (p1x != p2x && p3x != p4x)
                    {
                        //生成敌人
                        GameObject go3 = GameObject.Instantiate(Enemy[0], new Vector3(p1x, p1y, 0), Quaternion.identity) as GameObject;
                        go3.transform.SetParent(MapHolder);
                        //生成果子
                        GameObject go4 = GameObject.Instantiate(Food[0], new Vector3(p2x, p2y, 0), Quaternion.identity) as GameObject;
                        go4.transform.SetParent(MapHolder);
                        //生成石头
                        int RSton = Random.Range(0, 4);
                        GameObject go7 = GameObject.Instantiate(Ston[RSton], new Vector3(p3x, p3y, 0), Quaternion.identity) as GameObject;
                        go7.transform.SetParent(MapHolder);
                        //生成树
                        int RTree = Random.Range(0, 5);
                        GameObject go8 = GameObject.Instantiate(Tree[RTree], new Vector3(p4x, p4y, 0), Quaternion.identity) as GameObject;
                        go8.transform.SetParent(MapHolder);
                    }
                    else
                    {

                        //生成水
                        GameObject go6 = GameObject.Instantiate(Water[0], new Vector3(p1x, p1y, 0), Quaternion.identity) as GameObject;
                        go6.transform.SetParent(MapHolder);


                    }

                }


                //生成老巢
                GameObject go5 = GameObject.Instantiate(EnemyHome[0], new Vector3(Cols - 1, Rows - 1, 0), Quaternion.identity) as GameObject;
                go5.transform.SetParent(MapHolder);
            }

        }



    }
    public void ChangeMap(int x, int y)
    {
        
        //土地当前状态 1开垦 2种子
        if (SoilType == 0)
        {
            int z = Random.Range(0, 2);
            GameObject go = GameObject.Instantiate(Soil[z], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
            go.transform.SetParent(MapHolder);

            SoilType = 1;
        }
        else if (SoilType == 1)
        {
            //int z1 = Random.Range(0, 2);
            GameObject go = GameObject.Instantiate(Seed[0], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
            go.transform.SetParent(MapHolder);

            SoilType = 2;

        }
        rest++;

    }

    public void ChangeFood()
    {

        GameObject[] a = GameObject.FindGameObjectsWithTag("seed");
        if (a == null)
        {
            print("没有种子");
        }
        else
        {
            
            foreach (GameObject a1 in a)
            {
                Vector2 postion = a1.transform.position;

                postion1 = postion;
                
            }
            
                GameObject go20 = GameObject.Instantiate(Seed[1], postion1, Quaternion.identity) as GameObject;
                go20.transform.SetParent(MapHolder);
            
                
            



        }



    }
    public void ChangeFood1()
    {

        GameObject[] b = GameObject.FindGameObjectsWithTag("seed1");
        if (b == null)
        {
            print("没有种子");
        }
        else
        {
            foreach (GameObject a2 in b)
            {
                Vector2 postion = a2.transform.position;

                postion2 = postion;
                GameObject go22 = GameObject.Instantiate(Seed[2], postion2, Quaternion.identity) as GameObject;
                go22.transform.SetParent(MapHolder);
            }
            

        }
    }
    public void ChangeFood2()
    {

        GameObject[] c = GameObject.FindGameObjectsWithTag("seed2");
        if (c == null)
        {
            print("没有种子");
        }
        else
        {
            foreach (GameObject a3 in c)
            {
                Vector2 postion = a3.transform.position;

                postion3 = postion;

            }
            GameObject go21 = GameObject.Instantiate(Seed[3], postion3, Quaternion.identity) as GameObject;
            go21.transform.SetParent(MapHolder);

        }
    }
    
}
